package xkp.Lesson6;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;


public class JobManagerUi extends JFrame {
    //当前页码 默认为0
    private int currentPageNum = 1;
    //每一页显示条目数
    private int pageSize = 30;
    //查询工作的关键字
    private String searchKw = "";
    //城市代码
    private String searchCity = "";

    //总页数
    private int allPageNum;
    private JobInfoDao jobInfoDao = new JobInfoDao();

    private JTextField jobText;
    private JTextField cityText;
    private JTextField pageNumText;
    private JTable jTable;
    private JScrollPane jScrollPane;


    public JobManagerUi() throws SQLException {
        this.setSize(1300,700);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        int count = jobInfoDao.getDataCount();
        allPageNum = count/pageSize;
        if(count%pageSize != 0){
            allPageNum++;
        }
        loadHead();
        loadTable();
        loadButtom();
        this.setVisible(true);
    }

    private void loadTable() throws SQLException {
        jTable = new JTable();
        jTable.setFont(new Font("楷体",Font.PLAIN,20));
        jTable.setRowHeight(30);
        jTable.getTableHeader().setFont(new Font("黑体",Font.BOLD,24));
        loadTableData(currentPageNum,pageSize,searchKw,searchCity);
        //此处一定要把jtable作为参数创建jScrollPane，否则表格不显示
        jScrollPane = new JScrollPane(jTable);
        this.add(jScrollPane,BorderLayout.CENTER);

    }

    private void loadTableData(int currentPageNum,int pageSize,String searchKw,String searchCity) throws SQLException {
        Vector<String> head = new Vector<>();
        head.add("ID");
        head.add("职位");
        head.add("薪资");
        head.add("学历");
        head.add("经验");
        head.add("地址");
        head.add("公司名称");
        Vector<Vector<String>> body = new Vector<>();
        //List<JobInfoEntity> list = jobInfoDao.selectAll();
        List<JobInfoEntity> list = jobInfoDao.selectByPage(currentPageNum,pageSize,searchKw,searchCity);
        for(JobInfoEntity job:list){
            Vector<String> vector = new Vector<>();
            vector.add(job.getJobId());
            vector.add(job.getJobName());
            vector.add(job.getJobSalary());
            vector.add(job.getJobEducation());
            vector.add(job.getJobExprience());
            vector.add(job.getJobArea());
            vector.add(job.getCompanyName());
            body.add(vector);
        }
        DefaultTableModel model = new DefaultTableModel(body,head);
        jTable.setModel(model);
    }

    private void loadButtom() {
        JPanel southPanel = new JPanel();

        JButton preBtn = new JButton("上一页");
        preBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(currentPageNum == 1){
                    JOptionPane.showMessageDialog(null,
                            "当前已经是第一页");
                    return;
                }
                currentPageNum--;
                try {
                    loadTableData(currentPageNum,pageSize,searchKw,searchCity);
                    pageNumText.setText(currentPageNum+"/"+allPageNum);
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });
        southPanel.add(preBtn);

        pageNumText = new JTextField();
        //设置尺寸
        pageNumText.setPreferredSize(new Dimension(100,30));
        //文字在文本框中居中
        pageNumText.setHorizontalAlignment(SwingConstants.CENTER);
        pageNumText.setText(currentPageNum+"/"+allPageNum);
        southPanel.add(pageNumText);

        JButton nextBtn = new JButton("下一页");
        nextBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(currentPageNum == allPageNum){
                    JOptionPane.showMessageDialog(null,
                            "当前已经是最后一页");
                    return;
                }
                currentPageNum++;
                try {
                    loadTableData(currentPageNum,pageSize,searchKw,searchCity);
                    pageNumText.setText(currentPageNum+"/"+allPageNum);
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });


        southPanel.add(nextBtn);



        this.add(southPanel,BorderLayout.SOUTH);
    }

    private void loadHead() {
        JPanel northPanel = new JPanel();

        JLabel jLabelJob = new JLabel("职位");

        northPanel.add(jLabelJob);
        jobText = new JTextField();
        jobText.setPreferredSize(new Dimension(200,30));
        northPanel.add(jobText);

        JLabel jLabelCity = new JLabel("城市");
        //jLabelCity.setFont(new Font("黑体", Font.BOLD, 24));
        northPanel.add(jLabelCity);
        cityText = new JTextField("");
        cityText.setPreferredSize(new Dimension(200,30));
        northPanel.add(cityText);
        JButton beginBtn = new JButton("查询");
        beginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //对工作关键字赋值  留给上一页和下一页可以继续使用
                searchKw = jobText.getText();
                //对城市编码赋值    留给上一页和下一页可以继续使用
                searchCity = cityText.getText();
                //当前页码变为1
                currentPageNum = 1;
                try {
                    loadTableData(currentPageNum,pageSize,searchKw,searchCity);
                    pageNumText.setText(currentPageNum+"/"+allPageNum);
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });
        northPanel.add(beginBtn);

        this.add(northPanel,BorderLayout.NORTH);
    }
}
